<template>
  <div>
    <el-card>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="formInline.categoryName" placeholder="请输入分类名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="formInline.type"
            placeholder="状态"
            clearable
            style="width: 192px"
          >
            <el-option
              v-for="item in categoryTypes"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery" v-if="$usePermission('admin:product:query')">查询</el-button>
          <el-button type="warning" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>

      <div class="btn-list">
        <el-button @click="addMenu" v-if="$usePermission('admin:product:save')">新建商品分类</el-button>
      </div>

      <el-table
        :data="tableData"
        style="width: 100%"
        row-key="id"
        border
        lazy
        :tree-props="{ children: 'children' }"
      >
        <el-table-column prop="categoryName" label="分类名称"/>
        <el-table-column prop="iconUrl" label="分类图标" align="center">
          <template #default="scope">
            <el-image 
                style="width: 50px;height: 50px;"
                :src="scope.row.iconUrl" 
                :zoom-rate="1.2"
                :max-scale="7"
                :min-scale="0.2"
                :preview-src-list="[scope.row.iconUrl]"
                preview-teleported
                fit="cover"/>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" align="center" />
        <el-table-column prop="isShow" label="状态" align="center">
          <template #default="scope">
            <el-tag :type="categoryTypes.find(item => scope.row.isShow === item.type).tagType">{{ categoryTypes.find(item => scope.row.isShow === item.type).name }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" @click="handleChildrenEdit(scope.row.id)" v-if="scope.row.pid === 0 && $usePermission('admin:product:save')">
              添加子目录
            </el-button>
            <el-button size="small" @click="handleEdit(scope.row)" v-if="$usePermission('admin:product:update')">
              修改
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row)"
              v-if="$usePermission('admin:product:remove')"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <category-add-or-update ref="categoryAddOrUpdateRef" @refreshDataList="loadMenu"></category-add-or-update>
    </el-card>
  </div>
</template>

<script setup>
  import { ref, reactive } from 'vue'
  import { tableList, batchRemove } from '~/api/product/category'
  import CategoryAddOrUpdate from './category-add-or-update.vue'
  import { showMessage } from '~/utils/my-notification'
  import { ElMessageBox } from 'element-plus'

  const categoryAddOrUpdateRef = ref(null)

  const formInline = reactive({
    categoryName: '',
    type: ''
  })

  const categoryTypes = [
    { type: 1, name: '显示', tagType: 'primary' },
    { type: 0, name: '隐藏', tagType: 'success' }
  ]

  const tableData = ref([])

  loadMenu()
  // 查询
  function onQuery() {
    loadMenu()
  }
  // 重置
  function onReset() {}
  // 新建菜单
  function addMenu() {
    if(categoryAddOrUpdateRef.value) {
      categoryAddOrUpdateRef.value.init('add')
    }
  }
  // 加载菜单
  function loadMenu() {
    tableList(formInline.type, formInline.name).then(res => {
      if(res && res.data.code === 200) {
        tableData.value = res.data.data
      }
    })
  }
  // 修改
  function handleEdit(row) {
    if(categoryAddOrUpdateRef.value) {
      categoryAddOrUpdateRef.value.init('update', row.id)
    }
  }
  // 添加子目录
  function handleChildrenEdit(id) {
    if(categoryAddOrUpdateRef.value) {
      categoryAddOrUpdateRef.value.init('add_children', id)
    }
  }
  // 删除
  function handleDelete(row) {
    ElMessageBox.confirm(`确定进行删除操作?`, '提示',{
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      closeOnClickModal: false
    }).then(() => {
      batchRemove(row.id).then(res => {
        if(res && res.data.code === 200) {
          loadMenu()
          showMessage('删除成功!')
        }
      })
    }).catch(() => {

    })
  }
</script>

<style>
  .btn-list {
    margin-bottom: 20px;
  }
</style>
